package cn.qyd.atomic;

import sun.misc.Unsafe;

/**
 * @author 邱运铎
 * @date 2024-03-24 21:39
 */
public class UnSafeDemo02 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Unsafe unsafe = UnsafeDemo01.getUnsafe();
        int size = 8;
        long address = unsafe.allocateMemory(size);
        long address01 = unsafe.reallocateMemory(address, size * 2);
        System.out.println(address);
        System.out.println(address01);

        try {
            unsafe.setMemory(null, address, size, (byte) 1);
            for (int i = 0; i < 2; i++) {
                unsafe.copyMemory(null, address, null, address01 + size * i, 8);
            }
            System.out.println(unsafe.getInt(address));
            System.out.println(unsafe.getLong(address));
            System.out.println(Integer.toBinaryString(unsafe.getInt(address)));
            System.out.println(unsafe.getLong(address01));
        } finally {
            unsafe.freeMemory(address);
            unsafe.freeMemory(address01);
        }



    }
}
